<?php
/**
 * 专项城市基金-线下活动-明细
 * @author Administrator
 *
 */
class specityPODetailModel extends MY_Model{
    function specityPODetailModel(){
        parent::MY_Model();
        $this->table = 'SpecityPODetail';
    }
    /**
     * 更新父级金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($id){
        $this->CI->load->model('specityPOfflineModel');
        $this->CI->specityPOfflineModel->updateMoney($id);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['offlineId']) && $data['offlineId'] ? (int)$data['offlineId'] : 0;
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //明细类型
        $detailType = $this->config->item('offlineDetailType');
        //费用项目描述
        $descType = $this->config->item('offlineDetailDescType');
        foreach ($list as $k => $v){
            $list[$k]['typeStr'] = isset($detailType[$v['type']]) ? $detailType[$v['type']] : '';
            $list[$k]['descTypeStr'] = isset($descType[$v['type']][$v['descType']]) ? $descType[$v['type']][$v['descType']] : '';
            if ($v['descType'] == -1){
                $list[$k]['descTypeStr'] .= '-'.$v['descTypeAlias'];
            }
        }
        return $list;
    }
    /**
     * 获取父级汇总金额
     * @param int $offlineId
     */
    function getMoney($offlineId){
        $findList = $this->getList(array('offlineId'=>$offlineId));
        $data = array(
            'budgetApply' => 0
        );
        foreach($findList as $v){
            $data['budgetApply'] += $v['budgetApply'];
        }
        return $data;
    }
    /**
     * 根据条件提供的线下活动ID集查询各详细数据(主要给汇总表导数据用)
     * @param array $offlineActList 带键值的线下活动列表
     */
    function getMoneyDetailList($offlineList = array()){
        $findList = $this->getList(array('offlineId' => array('in',array_keys($offlineList))));
        $initData = array(
            'budgetApply' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'budgetAudit' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'settleApply' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'settleAudit' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
        );
        $returnList = array();
        foreach ($offlineList as $k => $v){
            $actId = (int)$v['id'];
            //初始化
            isset($returnList[$actId]) || $returnList[$actId] = $initData;
        }
        foreach($findList as $v){
            $actId = (int)$v['offlineId'];
            $key = '';
            if ($v['type'] == 1){//场馆配套费用
                $key = 'venue';
            }else if($v['type'] == 2){//活动组织费用
                $key = 'activity';
            }
            //预算申请金额
            $returnList[$actId]['budgetApply'][$key] += $v['budgetApply'];
            $returnList[$actId]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核金额
            if ($offlineList[$actId]['budgetAuditRs'] == 1){
                $returnList[$actId]['budgetAudit'][$key] += $v['budgetApply'];
                $returnList[$actId]['budgetAudit']['total'] += $v['budgetApply'];
            }
            //结算申请金额
            if($offlineList[$actId]['isSettle'] == 1){
                $returnList[$actId]['settleApply'][$key] += $v['budgetApply'];
                $returnList[$actId]['settleApply']['total'] += $v['budgetApply'];
                //结算审核金额
                if ($offlineList[$actId]['settleAuditRs'] == 1){
                    $returnList[$actId]['settleAudit'][$key] += $v['budgetApply'];
                    $returnList[$actId]['settleAudit']['total'] += $v['budgetApply'];
                }
            }
        }
        return $returnList;
    }
}